﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using PTTK19.BUS;
using System.Data.SqlClient;
using System.Transactions;

namespace PTTK19.DB
{
    class DB_PhieuXuatKho
    {
        public static int LayMaSoPXK()
        {
            DBMS dbms = new DBMS();
            dbms.Connect();
            string sql = "SELECT MAX(MaPhieuXuatKho) FROM PhieuXuatKho";
            int i = (int)dbms.ExecuteScalar(sql);
            dbms.Disconnect();
            return i;
        }
        public static int ThemPhieuXuat(BUS_PhieuXuatKho PXK, ArrayList arrSanPham, DataTable dtSP, ArrayList arrSoLuong)
        {
            using (var tran = new TransactionScope())
            {
                int i = 0;           
                DBMS dbms = new DBMS();
                dbms.Connect();           
                string sql = "insert into PhieuXuatKho values(" + PXK.MaPYC + ",'" + PXK.NgayKham + "','" + PXK.NgayGiao + "'," + PXK.TinhTrang + ")";
                i = dbms.ExecuteNoneQuery(sql);
                for (int j = 0; j < arrSanPham.Count - 1; j++)
                {
                    int MaSp = int.Parse(arrSanPham[j].ToString());
                    int soluong = int.Parse(arrSoLuong[j].ToString());
                    string sql1 = "insert into ChiTietPhieuXuatKho values(" + PXK.MaPXK + "," + MaSp + "," + soluong + "," + PXK.TinhTrang + ")";
                    i = dbms.ExecuteNoneQuery(sql1);
                  }
                ///////
                string sql2 = " SELECT MaSP,SoLuongTon FROM Kho ";
                dbms.UpdateDatatTable(dtSP, sql2);
                //////
                string sql3 = "UPDATE PhieuYeuCau SET TinhTrang = 1 WHERE MaPhieuYC =" + PXK.MaPYC;
                i = dbms.ExecuteNoneQuery(sql3);
                dbms.Disconnect();

                tran.Complete();
                return i;
            }
        }
        public void HienThiReport()
        {
            
        }

    }
}
